package com.csust.automaticteach.app.teacher;

import com.csust.automaticteach.app.advisor.MyLoggerAdvisor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.stereotype.Component;

import static com.csust.automaticteach.constant.AppPromptConstant.LESSON_SYSTEM_ROLE;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * @ClassNamePrepareLessonApp
 * @Description 备课助手
 * @Author钰玟
 * @Date2025/6/22 下午7:22
 * @Version 1.0
 **/
@Component
@Slf4j
public class PrepareLessonApp {

    private ChatClient chatClient;

    public PrepareLessonApp(ChatModel dashscopeChatModel) {
        //开启内存级别的会话记忆
        ChatMemory chatMemory = new InMemoryChatMemory();

        this.chatClient = ChatClient.builder(dashscopeChatModel)//模型选择
                .defaultSystem(LESSON_SYSTEM_ROLE)//系统预设
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory))//开启会话记忆
                .defaultAdvisors(new MyLoggerAdvisor())//日志处理
                .build();
    }

    public String automaticPrepareLesson(String context, String chatId) {
        ChatResponse chatResponse = chatClient.prompt()
                .user(context)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId).param(
                        CHAT_MEMORY_RETRIEVE_SIZE_KEY, 5))
                .call()
                .chatResponse();
        String content = chatResponse.getResult().getOutput().getText();
//        log.info("content:{}",  content);
        return content;
    }
}
